# 法一
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        res = []
        m, n = len(word1), len(word2)
        i = j = 0
        while i < m or j < n:
            # 判断是否需要追加
            if i == m:
                res.append(word2[j])
                j += 1
                continue
            elif j == n:
                res.append(word1[i])
                i += 1
                continue
            
            # 交替合并
            control = (i + j) % 2
            if control == 0:
                res.append(word1[i])
                i += 1
            else:
                res.append(word2[j])
                j += 1
        return (''.join(res))
                
            
#法一优化
class Solution:
    def mergeAlternately(self, word1: str, word2: str) -> str:
        res = []
        m, n = len(word1), len(word2)
        i = j = 0
        while i < m or j < n:
            if i < m:
                res.append(word1[i])
                i += 1
            if j < n:
                res.append(word2[j])
                j += 1
        return (''.join(res))
                
            
